﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Project.Teller
{
    public partial class CreateOrder : System.Web.UI.Page
    {
        DataPOFDataContext db;
        protected void Page_Load(object sender, EventArgs e)
        {
            DataPOFDataContext db = new DataPOFDataContext();
            string acc = (from x in db.AccountLogins where x.Username == Session["username"].ToString() select x.FullName).FirstOrDefault();
            Label1.Text = acc;
            int ID = (from x in db.OrderDetails select x.Id).Count();
            TextBox1.Text = Convert.ToString(ID + 1);
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["username"] = null;
            Response.Cache.SetNoStore();
            Response.CacheControl = "no-cache";
            Response.Redirect("../Home/Login.aspx");
        }
      
        protected void Button2_Click(object sender, EventArgs e)
        {
            DataPOFDataContext data2 = new DataPOFDataContext();

            OrderDetail orderDetail = new OrderDetail();



            ///test/////
            string route = DropDownList1.SelectedItem.ToString() + DropDownList2.SelectedItem.ToString();
            int routid = (from x in data2.Routes where x.Route1 == route select x.ID).FirstOrDefault();
            ///
            
            // Add RouteID
            orderDetail.Id = Convert.ToInt32(TextBox4.Text);
            orderDetail.CustomerOrderID = Convert.ToInt32(DropDownList4.SelectedItem.ToString());

            orderDetail.RouteID = routid;
            routid = 1;
            int number2 = 0;
            if (DropDownList3.SelectedValue.ToString().Equals("normal"))
            {
                number2 = 1;
            }
            else if (DropDownList3.SelectedValue.ToString().Equals("fast"))
            {
                number2 = 2;
            }

            orderDetail.SeviceID = number2;
            number2 = 0;
            orderDetail.AccountID = Convert.ToInt32(DropDownList5.SelectedIndex.ToString());




            orderDetail.ReceiverName = TextBox2.Text;

            orderDetail.ReceiverIdentity = TextBox7.Text;

            orderDetail.ReceiverAddress = TextBox3.Text;

            orderDetail.ReceiverPhone = Convert.ToInt32(TextBox4.Text);

            orderDetail.ReceiverEmail = TextBox5.Text;

            orderDetail.Volume = Convert.ToInt32(TextBox6.Text);

            orderDetail.Price = Convert.ToDecimal(Label5.Text);

            //////////



            // Add

            try
            {
                
                data2.OrderDetails.InsertOnSubmit(orderDetail);

            }

            catch (Exception ex)
            {

                //MessageBox.Show(ex.Message);

            }

            finally
            {

                // Submit to data

                data2.SubmitChanges();

            }
        }

        protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            DataPOFDataContext db = new DataPOFDataContext();
            string route = DropDownList1.SelectedItem.ToString() + DropDownList2.SelectedItem.ToString();
            int routid = (from x in db.Routes where x.Route1 == route select x.ID).FirstOrDefault();

            int Distance = (from x in db.RouteDetails where x.ID == routid select x.Distance).FirstOrDefault();
            Label1.Text = Distance.ToString();
            decimal DistancePrice = (from x in db.PriceOfVolumes where x.ID == 1 select x.Distance).FirstOrDefault();
            int a = Convert.ToInt32(DistancePrice) * Distance;

            if (Label1.Text == "0")
            {
                Label2.Text = "Free";
                double b = Convert.ToDouble(Label3.Text);
                double c = Convert.ToDouble(Label4.Text);
                Label5.Text = Convert.ToString(c + b);
            }
            else
            {
                Label2.Text = a.ToString();
                double b = Convert.ToDouble(Label3.Text);
                double c = Convert.ToDouble(Label4.Text);
                Label5.Text = Convert.ToString(a + b + c);
            }
        }

        protected void DropDownList2_SelectedIndexChanged1(object sender, EventArgs e)
        {
            DataPOFDataContext db = new DataPOFDataContext();
            string route = DropDownList1.SelectedItem.ToString() + DropDownList2.SelectedItem.ToString();
            int routid = (from x in db.Routes where x.Route1 == route select x.ID).FirstOrDefault();
            int Distance = (from x in db.RouteDetails where x.ID == routid select x.Distance).FirstOrDefault();

            Label1.Text = Distance.ToString();
            decimal DistancePrice = (from x in db.PriceOfVolumes where x.ID == 1 select x.Distance).FirstOrDefault();
            int a = Convert.ToInt32(DistancePrice) * Distance;
            if (Label1.Text == "0")
            {
                Label2.Text = "Free";
                double b = Convert.ToDouble(Label3.Text);
                double c = Convert.ToDouble(Label4.Text);
                Label5.Text = Convert.ToString(c + b);
            }
            else
            {
                Label2.Text = a.ToString();
                double b = Convert.ToDouble(Label3.Text);
                double c = Convert.ToDouble(Label4.Text);
                Label5.Text = Convert.ToString(a + b + c);
            }
        }

        protected void DropDownList3_SelectedIndexChanged1(object sender, EventArgs e)
        {
            DataPOFDataContext db = new DataPOFDataContext();
            string z = DropDownList3.SelectedValue.ToString();
            decimal ServicePrice = (from x in db.Services where x.Service1 == z select x.Price).FirstOrDefault();
            Label4.Text = Convert.ToString(ServicePrice);
            double a = Convert.ToDouble(Label2.Text);
            double b = Convert.ToDouble(Label3.Text);
            double c = Convert.ToDouble(ServicePrice);
            Label5.Text = Convert.ToString(a + b + c);
        }

        protected void TextBox6_TextChanged(object sender, EventArgs e)
        {
            DataPOFDataContext db = new DataPOFDataContext();
            decimal Distance = (from x in db.PriceOfVolumes select x.Volume).FirstOrDefault();
            decimal z = 0;

            if (Convert.ToDouble(TextBox6.Text) < 1)
            {
                z = Convert.ToDecimal(TextBox6.Text) * Distance;
                Label3.Text = z.ToString();
                double a = Convert.ToDouble(Label2.Text);
                double c = Convert.ToDouble(Label4.Text);
                Label5.Text = Convert.ToString(a + 1000 + c);
            }
            else
            {
                z = Convert.ToDecimal(TextBox6.Text) * Distance;
                Label3.Text = z.ToString();
                double a = Convert.ToDouble(Label2.Text);
                double c = Convert.ToDouble(Label4.Text);
                Label5.Text = Convert.ToString(a + c + Convert.ToDouble(z));
            }
        }
    }
}